package com.lyb.server.hb.demo;


import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.lyb.server.hb.utils.ConverterUtil;
import com.lyb.server.hb.utils.HttpFileUtil;
import com.lyb.server.hb.utils.RSAUtil;

import java.io.File;
import java.util.HashMap;
import java.util.Map;


/**
 * 手机POS示例
 *
 * @author Administrator
 */
public class MposDemo {

//		private static final String url = "http://127.0.0.1:9901";//请求地址
    private static final String url = "https://mamipay.com";//请求地址--测试
//	  private static final String url = "https://pay.erpyt.com";//请求地址--生产


    // 加密验签的公钥--测试
    private static final String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDWQWY3KvQ9iBfUHQTF2L5YQsaDR+pWJ/3dOoMuXr6rQ3qQSHxssYl6FLjmMBJ6LxVEDRnA+K81lm1QPHEs5cirF2MfJ0Een2OoNG3iDk3VIcfAVZyPwBKHct9RjM95t3mr6g0WsLxnTsWZ0mQuEdS20KYkk2uGFG4zrd/NFnB3HQIDAQAB";
    // 签名解密的私钥--测试
    private static final String privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBANZBZjcq9D2IF9QdBMXYvlhCxoNH6lYn/d06gy5evqtDepBIfGyxiXoUuOYwEnovFUQNGcD4rzWWbVA8cSzlyKsXYx8nQR6fY6g0beIOTdUhx8BVnI/AEody31GMz3m3eavqDRawvGdOxZnSZC4R1LbQpiSTa4YUbjOt380WcHcdAgMBAAECgYAo0qcuJcIJo0+e+7R4GfcSjA6U3vP4XIBTUZ3DDQxZR28n5JfIgf9GwoIEYe5zV1s+7xwUF44SsMm9Ovb5bgLQtU9w28VPepEQ4p2dgx1dFU8lCnmewVCuGTaxAwvRL9DNfa++PxXiLZBrXcNykcW+0/iH gF7+dt1qgKGHnzbTgQJBAO2WvZU7x3/Cn5JEQJHDFJ+4RTfwB4ySl8GLTR37G3+E tAADA+W5Uz1l/C6CJKPMCp6iyyGgD+/twekRJFKKVeECQQDm28YvwSsW1+LQpjw3 Kwb6UQl24dSsJB92g6Mjl90mWLN4hMuyrJ5xTrQgWaz8H0C2dvzXktxYNWGsnXNtERC9AkASOz//BKBRcHjYO36M8OBTRUuieDvMEK+ufWcEhvFcPq2lVpXk+eymSH4qf3gaTZz/AVT/Fo5ChgccQGF+J/lhAkA49bzeh4DEMBwMcxekXP9pv5jgNjQtDPSeXzhfTdcI4RXXBJzlxuWI+2xCFx4xTty+qHkWD2xCb1lcJ7UVEgLZAkAYT3zyA5jc0/fdLnYCYF3457WPJr4hlbv5S2RGDrRi+v5dlbD1OJQbVmCEdetkaCBIiehDIkatqMITTctnJZQL";

    private static String idNo = "888888";//厂商识别号
    private static String agentCode = "HB_A202306240005";//商户标识
    private static String merchantCode = "HB_M202308060003";//商户标识


    public static void main(String[] args) throws Exception {



//      saveMerchant();//创建商户

//      incomingMerchant();//商户进件

//      uploadPicture(new File("C:\\Users\\Administrator\\Desktop\\新建文件夹 (2)\\门头照.jpeg"));

//      mcc();//获取pos商户mcc

//      savePosOrder(); //手机pos下单

//      queryPay();//订单查询

//      rate();//获取pos商户可配置最大最小费率

//      updateRate();//修改商户费率

//      updateMerchant();//修改商户信息

        System.out.println("main方法执行完成...");
    }

    private static void updateMerchant() throws Exception {
        JSONObject jsonParams = new JSONObject();
        JSONObject data = new JSONObject();
        data.put("merchantCode", "HB_M202309260002");//商户标识进件创建商户成功后返回
        data.put("credentialCode", "13052119971104777X");//身份证号
        data.put("provinceCode","130000");//商户所在省份Code
        data.put("cityCode", "130500");//商户所在城市Code
        data.put("districtCode", "130502");//商户所在县区Code
        data.put("province", "河北省");//商户所在省份
        data.put("city", "邢台市");//商户所在城市
        data.put("district", "桥东区");//商户所在县区
        data.put("bankProvince", "河北省");//开户行省份
        data.put("bankCity", "邢台市");//开户行城市不得为空
        data.put("bankDistrict", "桥东区");//开户行县区
        data.put("phone", "13568742147");//银行预留手机号
        data.put("bankHeadOffice", "105131000010");//银行联行号
        data.put("bankDetail", "中国建设银行股份有限公司邢台分行");//开户行网点
        data.put("bankAccountName", "测试");//银行开户名
        data.put("bankAccount", "62171259874210194");//银行卡号 测试换成真实数据
        data.put("merchantName", "建叶测试");//商户名称
        data.put("addrDetail", "恒大城");//联系地址
        data.put("linkPerson", "杨建叶");//联系人
        data.put("longitude", "114.49229");//经度
        data.put("latitude", "37.070451");//纬度
        data.put("idFrontUrl", "https://hb-data.obs.cn-north-9.myhuaweicloud.com/81fac16a126d427dbac908bf4b727ba.png");//证件照正面
        data.put("idAfterUrl", "https://hb-data.obs.cn-north-9.myhuaweicloud.com/1c624f5ad3ba46539ee7205899cff18.png");//证件照反面
        data.put("indoorUrl", "https://hb-data.obs.cn-north-9.myhuaweicloud.com/f4542513b966468ba0ab4f00bc570c2.jpeg");//经营场所照片
        data.put("bankCardFrontUrl", "https://hb-data.obs.cn-north-9.myhuaweicloud.com/69fca29cb1a840b956a08e62c05ae1c.jpg");//银行卡背面照片
        data.put("bankCardAfterUrl", "https://hb-data.obs.cn-north-9.myhuaweicloud.com/ae3cb369d4c046fbd38064cb128376a.jpeg");//银行卡背面照片
        data.put("tradeTerminalNo", "111111111111");//设备终端号 手机对应唯一指纹
        data.put("serviceCode", "202303010001");//serviceCode
        data.put("idCardValidityPeroidStart", "2021-02-01");//身份证有效期开始时间不得为空
        data.put("idCardValidityPeroidEnd", "2099-02-01");//身份证有效期结束时间不得为空
        data.put("idHoldUrl", "https://hb-data.obs.cn-north-9.myhuaweicloud.com/ae3cb369d4c046fbd38064cb128376a.jpeg");//证件手持照
        data.put("cashierUrl", "https://hb-data.obs.cn-north-9.myhuaweicloud.com/ae3cb369d4c046fbd38064cb128376a.jpeg");//门店收银台照片
        data.put("doorheadUrl", "https://hb-data.obs.cn-north-9.myhuaweicloud.com/ae3cb369d4c046fbd38064cb128376a.jpeg");//门店门头照片
        data.put("mcc", "5441");//mcc
        data.put("idNo", idNo);//厂商识别号
        System.out.println("data序列化后:" + data.toString());
        String sign = RSAUtil.sign(data.toString(), privateKey);//使用自己的私钥进行签名
        System.out.println("sign签名：\n" + sign);
        jsonParams.put("data", data.toString());
        jsonParams.put("sign", sign);
        //使用系统公钥(航邦公钥)加密
        String encryptBody = RSAUtil.encrypt(jsonParams.toString(), getPublicKey());
        Map<String, Object> param = new HashMap<>();
        param.put("param", encryptBody);
        System.out.println(url + "/api-order-receipt/order/merchant/updateMerchant");
        String res = HttpUtil.post(url + "/api-order-receipt/order/merchant/updateMerchant", param, 60000);
        JSONObject resJson = ConverterUtil.toJSON(res);
        System.out.println("=============《响应数据》==============\n" + resJson);
        if ("0000".equals(resJson.get("code")) && resJson.get("data") != null) {
            String encryptDataStr = resJson.get("data").toString();//加密的data数据
            String sign2 = resJson.get("sign").toString();//签名串
            //使用自己的私钥进行解密数据   注: 数据传输时 “+” 号会替换为空格， 解密时 需把空格替换回“+”号
            String paramJsonStr = RSAUtil.decrypt(encryptDataStr.replaceAll(" ", "+"), privateKey);
            System.out.println("解密后的data\n" + paramJsonStr);
            //将解密出来的数据使用系统公钥(航邦公钥)进行验签
            boolean verify = RSAUtil.verifySign(paramJsonStr, sign2, getPublicKey());
            System.out.println("验签结果：" + verify);
        }

    }

    private static void incomingMerchant() throws Exception {
        JSONObject jsonParams = new JSONObject();
        JSONObject data = new JSONObject();
        data.put("merchantCode", "HB_M202309260002");//商户标识进件创建商户成功后返回
        data.put("credentialCode", "13052119971104777X");//身份证号
        data.put("provinceCode","130000");//商户所在省份Code
        data.put("cityCode", "130500");//商户所在城市Code
        data.put("districtCode", "130502");//商户所在县区Code
        data.put("province", "河北省");//商户所在省份
        data.put("city", "邢台市");//商户所在城市
        data.put("district", "桥东区");//商户所在县区
        data.put("bankProvince", "河北省");//开户行省份
        data.put("bankCity", "邢台市");//开户行城市不得为空
        data.put("bankDistrict", "桥东区");//开户行县区
        data.put("phone", "13568742147");//银行预留手机号
        data.put("bankHeadOffice", "105131000010");//银行联行号
        data.put("bankDetail", "中国建设银行股份有限公司邢台分行");//开户行网点
        data.put("bankAccountName", "测试");//银行开户名
        data.put("bankAccount", "62171259874210194");//银行卡号 测试换成真实数据
        data.put("merchantName", "建叶测试");//商户名称
        data.put("addrDetail", "恒大城");//联系地址
        data.put("linkPerson", "杨建叶");//联系人
        data.put("longitude", "114.49229");//经度
        data.put("latitude", "37.070451");//纬度
        data.put("idFrontUrl", "https://hb-data.obs.cn-north-9.myhuaweicloud.com/81fac16a126d427dbac908bf4b727ba.png");//证件照正面
        data.put("idAfterUrl", "https://hb-data.obs.cn-north-9.myhuaweicloud.com/1c624f5ad3ba46539ee7205899cff18.png");//证件照反面
        data.put("indoorUrl", "https://hb-data.obs.cn-north-9.myhuaweicloud.com/f4542513b966468ba0ab4f00bc570c2.jpeg");//经营场所照片
        data.put("bankCardFrontUrl", "https://hb-data.obs.cn-north-9.myhuaweicloud.com/69fca29cb1a840b956a08e62c05ae1c.jpg");//银行卡背面照片
        data.put("bankCardAfterUrl", "https://hb-data.obs.cn-north-9.myhuaweicloud.com/ae3cb369d4c046fbd38064cb128376a.jpeg");//银行卡背面照片
        data.put("tradeTerminalNo", "111111111111");//设备终端号 手机对应唯一指纹
        data.put("serviceCode", "202303010001");//serviceCode
        data.put("idCardValidityPeroidStart", "2021-02-01");//身份证有效期开始时间不得为空
        data.put("idCardValidityPeroidEnd", "2099-02-01");//身份证有效期结束时间不得为空
        data.put("idHoldUrl", "https://hb-data.obs.cn-north-9.myhuaweicloud.com/ae3cb369d4c046fbd38064cb128376a.jpeg");//证件手持照
        data.put("cashierUrl", "https://hb-data.obs.cn-north-9.myhuaweicloud.com/ae3cb369d4c046fbd38064cb128376a.jpeg");//门店收银台照片
        data.put("doorheadUrl", "https://hb-data.obs.cn-north-9.myhuaweicloud.com/ae3cb369d4c046fbd38064cb128376a.jpeg");//门店门头照片
        data.put("mcc", "5441");//mcc
        data.put("idNo", idNo);//厂商识别号
        System.out.println("data序列化后:" + data.toString());
        String sign = RSAUtil.sign(data.toString(), privateKey);//使用自己的私钥进行签名
        System.out.println("sign签名：\n" + sign);
        jsonParams.put("data", data.toString());
        jsonParams.put("sign", sign);
        //使用系统公钥(航邦公钥)加密
        String encryptBody = RSAUtil.encrypt(jsonParams.toString(), getPublicKey());
        Map<String, Object> param = new HashMap<>();
        param.put("param", encryptBody);
        System.out.println(url + "/api-order-receipt/order/merchant/incoming");
        String res = HttpUtil.post(url + "/api-order-receipt/order/merchant/incoming", param, 60000);
        JSONObject resJson = ConverterUtil.toJSON(res);
        System.out.println("=============《响应数据》==============\n" + resJson);
        if ("0000".equals(resJson.get("code")) && resJson.get("data") != null) {
            String encryptDataStr = resJson.get("data").toString();//加密的data数据
            String sign2 = resJson.get("sign").toString();//签名串
            //使用自己的私钥进行解密数据   注: 数据传输时 “+” 号会替换为空格， 解密时 需把空格替换回“+”号
            String paramJsonStr = RSAUtil.decrypt(encryptDataStr.replaceAll(" ", "+"), privateKey);
            System.out.println("解密后的data\n" + paramJsonStr);
            //将解密出来的数据使用系统公钥(航邦公钥)进行验签
            boolean verify = RSAUtil.verifySign(paramJsonStr, sign2, getPublicKey());
            System.out.println("验签结果：" + verify);
        }

    }


    /**
     *
     * 图片上传
     * @param file
     * @return
     */

    public static JSONObject uploadPicture(File file) {
         String   url = "https://mamipay.com/api-file/files-anon";//测试路径
//         String   url = "https://pay.erpyt.com/api-file/files-anon";//生产路径
        String resp = HttpFileUtil.doPostUploadFile(url,file);
        JSONObject jsonObject = JSONObject.parseObject(resp);
        System.out.println("图片上传返回值"+jsonObject);
        return jsonObject;
    }

    /**
     * 获取系统公钥(航邦公钥)
     * 公钥是不变的,获取后请自行保存
     *
     * @throws Exception
     */
    public static String getPublicKey() {
        Map<String, Object> param = new HashMap<>();
        String res = HttpUtil.post(url + "/api-order-receipt/order/api/get_public_key", param, 60000);
        JSONObject resJsonStr = ConverterUtil.toJSON(res);
//		System.out.println("=============《获取系统公钥 响应数据》==============\n"+resJsonStr);
        /**
         * 响应示例
         * {"code":"0000","msg":"success","data":"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiSecPJOQnhaepE1v1lTfUdW5paAbO37Ff9G6GeN25REikHYqKKW1aRNYXfrhLpty8oRcOZWd98Im7o/kQDKpcemlsRkgqRvqOqaEyy8DFNRXR0kJ9wTPafgjho/GN71K3G9uHhWTX3ejbdW5s37a60t/7Cv0utatRs1TLpiFMBFULEFYcEDGuwuTUASSORRH6ymcxHDdUb+Tc0S0AZIg7zdS7cKqNsuZAPglBLgZZOAtU/xgERSrkInCvXh/V/1AYG96jTCQ0+Ky/kXFzd+p8r21E+HGm9rNP3kcMhOWaBYi39zE2Bw31DojE4PMShY08tYZqT0nOIiLoZypLnrQ5wIDAQAB","sign":""}
         */
        return (String) resJsonStr.get("data");
    }



    /**
     * 创建商户
     *
     */
    public static void saveMerchant() throws Exception {
        JSONObject jsonParams = new JSONObject();
        JSONObject data = new JSONObject();
        data.put("agentCode",agentCode);//代理商标识
        data.put("contacts", "123");//联系人
        data.put("phone","15358789210");//联系电话不得为空
        data.put("address", "测试地址就是测试一哈");//详细地址
        data.put("longitude", "114.514809");//纬度
        data.put("latitude", "37.080255");//经度
        data.put("isComm", "1");//是否计算佣金
        data.put("idNo", idNo);//厂商识别号
        data.put("merchantName", "测试一哈");//商户名称

        System.out.println("data序列化后:" + data.toString());
        String sign = RSAUtil.sign(data.toString(), privateKey);//使用自己的私钥进行签名
        System.out.println("sign签名：\n" + sign);
        jsonParams.put("data", data.toString());
        jsonParams.put("sign", sign);
        //使用系统公钥(航邦公钥)加密
        String encryptBody = RSAUtil.encrypt(jsonParams.toString(), getPublicKey());
        Map<String, Object> param = new HashMap<>();
        param.put("param", encryptBody);
        System.out.println(url + "/api-order-receipt/order/merchant/save");
        String res = HttpUtil.post(url + "/api-order-receipt/order/merchant/save", param, 60000);
        JSONObject resJson = ConverterUtil.toJSON(res);
        System.out.println("=============《响应数据》==============\n" + resJson);
        if ("0000".equals(resJson.get("code")) && resJson.get("data") != null) {
            String encryptDataStr = resJson.get("data").toString();//加密的data数据
            String sign2 = resJson.get("sign").toString();//签名串
            //使用自己的私钥进行解密数据   注: 数据传输时 “+” 号会替换为空格， 解密时 需把空格替换回“+”号
            String paramJsonStr = RSAUtil.decrypt(encryptDataStr.replaceAll(" ", "+"), privateKey);
            System.out.println("解密后的data\n" + paramJsonStr);
            //将解密出来的数据使用系统公钥(航邦公钥)进行验签
            boolean verify = RSAUtil.verifySign(paramJsonStr, sign2, getPublicKey());
            System.out.println("验签结果：" + verify);
        }

        /**
         {"code":"0000","msg":"success","data":"cJOMgeyFPViXuboycUM2sm0CRoWoHWKcWfkmToq4nUevwnWZ9Cvlb7NwagLNIRzA1paZ4Uj9I0Y/iSevYj663rr7n7pnbwnFd2bLxD7cmASxeAdMLI3GbJKyh/IbzwqAUZR61S3Ed4Kic29dF1CQqpxhCgeKiE9IAcAVXRi7klNxtErw9JqcDXWoeu2Y5Tm4PigYzKhqBGMe4K84opeNJuMxZLuXlMUkWhfjrX3+HRdW7dQkpBYaFuamZPoeIbQ32SO1D75MXOyJwGKgABpe9YSYOn2xt4ez6xA7hjNdLUL1ly3H0oFmBDOdUJFe5wxkfvDibScQsIWr3xrt4waIYyvGHY046ozRhiZpb3Xle+6/MaFiiwOX/73j4TbMzIn7+6c5IqiIKbHxYRKVlP6si73EIvEvm2+fSmfLc5e0Bc9lJzVdgyikTCpep8vmqEbSFAn8B3wieuJVKFPnSAzRQJmu7/9foVbxMlo/5VZoeksU0OUw9/0A1CT7iOJJuPVJ","sign":"X4hdaaBo6yymLUrQjGKI2sOUHgDkafcqUOBUpFZv8dkqDzuph+B2c5TGAAHDFoprFIjgHrZgzrj/cZE4q2vT1YTz6UaWVjSIXy5WRvZAYxfGjKaUYi9EE0OQbXvyHLLW69/rPm/3m4OGvelQO9tkrBCU5+H8HkXTdFgE1SOidaI="}
         解密后的data
         {"createTime":"2023-09-26 16:41:36","updateTime":"2023-09-26 16:41:36","merchantName":"测试一哈","merchantCode":"HB_M202309260002","contacts":"123","phone":"15358789210","address":"测试地址就是测试一哈","longitude":"114.514809","latitude":"37.080255","isComm":"1"}
         验签结果：true
         main方法执行完成...
         */
    }


    /**
     *获取 mcc
     *
     */
    public static void mcc() throws Exception {
        JSONObject jsonParams = new JSONObject();
        JSONObject data = new JSONObject();
        data.put("idNo", idNo);//厂商识别号
        System.out.println("data序列化后:" + data.toString());
        String sign = RSAUtil.sign(data.toString(), privateKey);//使用自己的私钥进行签名
        System.out.println("sign签名：\n" + sign);
        jsonParams.put("data", data.toString());
        jsonParams.put("sign", sign);
        //使用系统公钥(航邦公钥)加密
        String encryptBody = RSAUtil.encrypt(jsonParams.toString(), getPublicKey());
        Map<String, Object> param = new HashMap<>();
        param.put("param", encryptBody);
        System.out.println(url + "/api-order-receipt/order/merchant/mcc");
        String res = HttpUtil.post(url + "/api-order-receipt/order/merchant/mcc", param, 60000);
        JSONObject resJson = ConverterUtil.toJSON(res);
        System.out.println("=============《响应数据》==============\n" + resJson);
        if ("0000".equals(resJson.get("code")) && resJson.get("data") != null) {
            String encryptDataStr = resJson.get("data").toString();//加密的data数据
            String sign2 = resJson.get("sign").toString();//签名串
            //使用自己的私钥进行解密数据   注: 数据传输时 “+” 号会替换为空格， 解密时 需把空格替换回“+”号
            String paramJsonStr = RSAUtil.decrypt(encryptDataStr.replaceAll(" ", "+"), privateKey);
            System.out.println("解密后的data\n" + paramJsonStr);
            //将解密出来的数据使用系统公钥(航邦公钥)进行验签
            boolean verify = RSAUtil.verifySign(paramJsonStr, sign2, getPublicKey());
            System.out.println("验签结果：" + verify);
        }
    }


    /**
     * 获取rate
     */
    public static void rate() throws Exception {
        JSONObject jsonParams = new JSONObject();
        JSONObject data = new JSONObject();
        data.put("idNo", idNo);//厂商识别号
        data.put("merchantCode", "HB_M202303050001");//商户标识
        System.out.println("data序列化后:" + data.toString());
        String sign = RSAUtil.sign(data.toString(), privateKey);//使用自己的私钥进行签名
        System.out.println("sign签名：\n" + sign);
        jsonParams.put("data", data.toString());
        jsonParams.put("sign", sign);
        //使用系统公钥(航邦公钥)加密
        String encryptBody = RSAUtil.encrypt(jsonParams.toString(), getPublicKey());
        Map<String, Object> param = new HashMap<>();
        param.put("param", encryptBody);
        System.out.println(url + "/api-order-receipt/order/merchant/rate");
        String res = HttpUtil.post(url + "/api-order-receipt/order/merchant/rate", param, 60000);
        JSONObject resJson = ConverterUtil.toJSON(res);
        System.out.println("=============《响应数据》==============\n" + resJson);
        if ("0000".equals(resJson.get("code")) && resJson.get("data") != null) {
            String encryptDataStr = resJson.get("data").toString();//加密的data数据
            String sign2 = resJson.get("sign").toString();//签名串
            //使用自己的私钥进行解密数据   注: 数据传输时 “+” 号会替换为空格， 解密时 需把空格替换回“+”号
            String paramJsonStr = RSAUtil.decrypt(encryptDataStr.replaceAll(" ", "+"), privateKey);
            System.out.println("解密后的data\n" + paramJsonStr);
            //将解密出来的数据使用系统公钥(航邦公钥)进行验签
            boolean verify = RSAUtil.verifySign(paramJsonStr, sign2, getPublicKey());
            System.out.println("验签结果：" + verify);
        }
        /**
         * {"code":"0006","msg":"商户标识不能为空","data":"","sign":""}
         * =============《响应数据》==============
         * {"code":"0000","msg":"success","data":"WoZ01SIK5udBQB9MGoS5r3c/sZwwUZtnz0Z+OUGhtYwDdDoAzvucd+WtdfRiTdoPBOUcvdM6HXlsbySnKF4IZ00K+BjLzKQQJweweBaqmiEZ2xUZ4TAWDeSnWUkfaCBeQc8QchzatUAnee2dJghMALXhAmTIM85FovHLXf2/Dbw=","sign":"MjPxPqc0B21EZEboN1OT6fH/NFi0ftjOj/+5MzQA+NYL57DVtSGe2TrP/BkbSJmwWZgPBaNLLi5fJNTDT4S3IXqub5guU1ZdxD4owubvTaZlic7CqCfg4WPz7QcPwhaVGBlUnaDRxwehMjAkRy7+efFR0Z8Dk0G4FRh0PttGkNA="}
         * 解密后的data
         * {"minRate":"0.55","maxRate":"0.63"}
         * 验签结果：true
         */
    }


    /**
     * 修改商户费率
     */
    public static void updateRate() throws Exception {
        JSONObject jsonParams = new JSONObject();
        JSONObject data = new JSONObject();
        data.put("idNo", idNo);//厂商识别号
        data.put("merchantCode", "HB_M202309220001");//商户标识
        data.put("rate", "0.58");//费率百分比
        System.out.println("data序列化后:" + data.toString());
        String sign = RSAUtil.sign(data.toString(), privateKey);//使用自己的私钥进行签名
        System.out.println("sign签名：\n" + sign);
        jsonParams.put("data", data.toString());
        jsonParams.put("sign", sign);
        //使用系统公钥(航邦公钥)加密
        String encryptBody = RSAUtil.encrypt(jsonParams.toString(), getPublicKey());
        Map<String, Object> param = new HashMap<>();
        param.put("param", encryptBody);
        System.out.println(url + "/api-order-receipt/order/merchant/rate");
        String res = HttpUtil.post(url + "/api-order-receipt/order/merchant/updateRate", param, 60000);
        JSONObject resJson = ConverterUtil.toJSON(res);
        System.out.println("=============《响应数据》==============\n" + resJson);
        if ("0000".equals(resJson.get("code")) && resJson.get("data") != null) {
            String encryptDataStr = resJson.get("data").toString();//加密的data数据
            String sign2 = resJson.get("sign").toString();//签名串
            //使用自己的私钥进行解密数据   注: 数据传输时 “+” 号会替换为空格， 解密时 需把空格替换回“+”号
            String paramJsonStr = RSAUtil.decrypt(encryptDataStr.replaceAll(" ", "+"), privateKey);
            System.out.println("解密后的data\n" + paramJsonStr);
            //将解密出来的数据使用系统公钥(航邦公钥)进行验签
            boolean verify = RSAUtil.verifySign(paramJsonStr, sign2, getPublicKey());
            System.out.println("验签结果：" + verify);
        }
        /**
         * {"code":"0006","msg":"商户标识不能为空","data":"","sign":""}
         =============《响应数据》==============
         {"code":"0000","msg":"success","data":"Wfm22cpNUBQaIY85uLWOlYv+aqVu6t3/3RZ5Z6PHnHhoKsD9De9GboICEYg6RG29WMnw5EiWjjh3NfqFOLEKL2rkbSHDsDHFde26exdzfLWswVbi4kadgFdAgK+bohiNBNElj0qAf5ZTlyy6I6lC0n05z51yNCgx1nYo4M9niB8=","sign":"eh28S0EjVZf2ZL+RQPaoD3x74/Ppvf8RuRCfd8WgyHrhoA/u/7Dw33VItVG81Eyp2YJWCTZ6Yp1LWs0Nc8dWQTYjR/PfNKgWIZMS8JE22J/WhyttkrfQkwqDJsQOohS0hRT+8Rv++9XKfRzY5M7aaob9Fn16qBL+u2ndhi7geDE="}
         解密后的data
         "成功"
         验签结果：true
         */
    }



    /**
     * 手机pos下单
     * 请求接口 "/api-order-receipt/order/api/mPosOrder"
     * @throws Exception
     */
    public static void savePosOrder() throws Exception {
        JSONObject jsonParams = new JSONObject();
        JSONObject data = new JSONObject();
        data.put("idNo", idNo);//厂商识别号
        data.put("merchantCode", merchantCode);//商户标识
        data.put("outTradeNo", System.currentTimeMillis());//商户订单号（由api接口调用方传入，唯一不可重复，不能为空）
        data.put("price", "0.02");//订单金额
        data.put("outNotifyUrl", "http://116.131.181.58:8080/api/order_callback");//支付结果异步通知回调地址
        data.put("outLiquidationNotifyUrl", "http://116.131.181.58:8080/api/order_callback");//支付结果异步通知回调地址
        data.put("remarks", "测试");//订单备注
        data.put("attach", "attach");//附加字段 原样返回

        String sign = RSAUtil.sign(data.toString(), privateKey);//使用自己的私钥进行签名
        System.out.println("sign签名：\n" + sign);
        jsonParams.put("data", data.toString());
        jsonParams.put("sign", sign);
        //使用系统公钥(航邦公钥)加密
        String encryptBody = RSAUtil.encrypt(jsonParams.toString(), getPublicKey());
        Map<String, Object> param = new HashMap<>();
        param.put("param", encryptBody);
        String res = HttpUtil.post(url + "/api-order-receipt/order/api/mPosOrder", param, 60000);
        JSONObject resJson = ConverterUtil.toJSON(res);
        System.out.println("=============《响应数据》==============\n" + resJson);
        if ("0000".equals(resJson.get("code")) && resJson.get("data") != null) {
            String encryptDataStr = resJson.get("data").toString();//加密的data数据
            String sign2 = resJson.get("sign").toString();//签名串
            //使用自己的私钥进行解密数据   注: 数据传输时 “+” 号会替换为空格， 解密时 需把空格替换回“+”号
            String paramJsonStr = RSAUtil.decrypt(encryptDataStr.replaceAll(" ", "+"), privateKey);
            System.out.println("解密后的data\n" + paramJsonStr);
            /**
             * TODO data示例 将接口返回报文传值给手机app,手机app根据此报文调起插件完成支付操作
             * {"channelKey":1,"checkInUrl":"https://mamipay.com/api-order-receipt/order/pos/terminalCheckIn","merchantCode":"HB_M202308060003","orderId":"1693869019998818306","payUrl":"https://mamipay.com/api-order-receipt/order/pos/posPay","price":0.02,"tradeTerminalNo":"70591142fe17c5aa95bd8b2d4a85ec03"}
             */
            //将解密出来的数据使用系统公钥(航邦公钥)进行验签
            boolean verify = RSAUtil.verifySign(paramJsonStr, sign2, getPublicKey());
            System.out.println("验签结果：" + verify);
        }
    }


    /**
     * 订单查询
     * @throws Exception
     */
    public static void queryPay() throws Exception{
        JSONObject jsonParams = new JSONObject();
        JSONObject data = new JSONObject();
        data.put("idNo", idNo);//厂商识别号
        data.put("merchantCode", merchantCode);//商户唯一标识
//		data.put("orderNo", "HB2023032216502600000001");//订单号 (orderNo和outTradeNo不可同时为空)
        data.put("outTradeNo", "1685703461094");//商户订单号（由api接口调用方传入，唯一不可重复，不能为空）

        String sign = RSAUtil.sign(data.toString(), privateKey);//使用自己的私钥进行签名
        System.out.println("sign签名：\n" + sign);
        jsonParams.put("data", data.toString());
        jsonParams.put("sign", sign);
        //使用系统公钥(航邦公钥)加密
        String publicKey = getPublicKey();
        String encryptBody = RSAUtil.encrypt(jsonParams.toString(), publicKey);
        Map<String, Object> param = new HashMap<>();
        param.put("param", encryptBody);
        String res = HttpUtil.post(url+"/api-order-receipt/order/api/query", param, 60000);
        JSONObject resJson = ConverterUtil.toJSON(res);
        System.out.println("=============《响应数据》==============\n"+resJson);
        if("0000".equals(resJson.get("code")) && resJson.get("data")!=null){
            String encryptDataStr = resJson.get("data").toString();//加密的data数据
            String sign2 = resJson.get("sign").toString();//签名串
            //使用自己的私钥进行解密数据   注: 数据传输时 “+” 号会替换为空格， 解密时 需把空格替换回“+”号
            String paramJsonStr = RSAUtil.decrypt(encryptDataStr.replaceAll(" ", "+"), privateKey);
            System.out.println("解密后的data\n"+paramJsonStr);
            /**
             * data示例
             *
             * {"code":"0006","msg":"参数错误,该订单不存在","data":"","sign":""}
             */
            //将解密出来的数据进行验签
            boolean verify = RSAUtil.verifySign(paramJsonStr, sign2, publicKey);
            System.out.println("验签结果："+verify);
        }
    }


}
